package Demo1;

import java.util.PriorityQueue;

/*
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-25
 * Time: 11:07
 */
public class Test {
    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        IntComparator cmp = new IntComparator();
        PriorityQueue<Integer> queue = new PriorityQueue<>(k,cmp);
        for (int i = 0; i < k; i++) {
            queue.offer(arr[i]);
        }
        for (int i = k; i < arr.length; i++) {
            int val = queue.peek();
            if(cmp.compare(arr[i],val) > 0){
                queue.poll();
                queue.offer(arr[i]);
            }
        }
        return ret;
    }
    public static void main(String[] args) {
        int[] array = {1,2,5,8,3};
        //1.堆排序 从小到大 创建大根堆
        //2.创建小根堆，边排边弹
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        queue.offer(1);
        queue.offer(7);
        queue.offer(3);
        queue.offer(4);
        queue.offer(2);
        System.out.println(queue);
        for (int i = 0; i < 3; i++) {
            System.out.println(queue.poll());
        }
    }
}
